package com.zdsoft.crm.controller.auth;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zdsoft.crm.entity.auth.SysFunction;
import com.zdsoft.crm.entity.auth.SysRole;
import com.zdsoft.crm.service.auth.ISysFunctionService;
import com.zdsoft.crm.service.auth.ISysRoleFunctionService;
import com.zdsoft.crm.service.auth.ISysRoleService;

@Controller
@RequestMapping("/auth/role_func")
public class RoleFunctionController {

	@Autowired
	ISysRoleFunctionService iSysRoleFunctionService;
	
	@Autowired
	ISysRoleService iSysRoleService;
	
	@Autowired
	ISysFunctionService iSysFunctionService;
	
	@GetMapping("/{roleId}")
	public ModelAndView save(@PathVariable("roleId")String roleId){
		ModelAndView mav=new ModelAndView("/auth/role_func/form.html");
		SysRole role=iSysRoleService.findById(roleId);
		mav.addObject("role",role);
		List<SysFunction> funcs=iSysFunctionService.findAll();
		mav.addObject("funcs",funcs);
		return mav;
	}
	
	@PostMapping("/{roleId}")
	@ResponseBody
	public void saveRoleFunc(@PathVariable("roleId")String roleId,String[] funcIds){
		iSysRoleFunctionService.save(roleId, funcIds);
		
	}
}
